package fr.fbhelp.parsing
{
	

	public class Toc
	{
		public static const TOC_TYPE_ADOBE_HHC_XML:String = "fr.fbhelp.parsing.TOC_TYPE_ADOBE_HHC_XML";
		public static const TOC_TYPE_ADOBE_HELPMAP_TXT:String = "fr.fbhelp.parsing.TOC_TYPE_HELPMAP";
		public static const TOC_TYPE_HAS_NO_SOURCE_ADOBE_TOC:String = "fr.fbhelp.parsing.TOC_TYPE_HAS_NO_SOURCE_ADOBE_TOC";
		
		private var _label:String;
		private var _folderName:String; 
		private var _basePath:String;
		private var _contents:String; 
		private var _type:String; 
		private var _generatedToc:String; 
		private var _generatedTocSection:String; 

		// [Deprecated] - Can be used for saving individual TOCs as separate files.
		private var _fileName:String; 

		public function Toc()
		{
		}

		public function parseFromContents():void
		{
			if (type == TOC_TYPE_ADOBE_HHC_XML)
			{
				createTocFromAdobeHhcXml();
			}
			
			if (type == TOC_TYPE_ADOBE_HELPMAP_TXT)
			{
				createTocFromAdobeHelpMapText();
			}
			
			if (type == TOC_TYPE_HAS_NO_SOURCE_ADOBE_TOC)
			{
				parseFromNone();
			}
			
			createTocSection();
		}
		
		/**
		 * Creates TOC string from HHC xml by simply replacing the tag and attribute names etc.
		 *   
		 * Source HHC XML by Adobe goes in this form:
		 *  
		 * <?xml version="1.0" encoding="UTF-8"?>
		 * <toc version="1.0">
		 *    <item name="Name" link="link.html"/>
		 *    <item name="Name" link="link.html">
		 *       <item name="Name" link="link.html"/>
		 *    </item>
		 * </toc>
		 * 
		 * Is must be converted to Eclipse TOC in Form:
		 * 
		 * <?xml version="1.0" encoding="UTF-8"?>
		 * <?NLS TYPE="org.eclipse.help.toc"?>
		 * <toc label="Name">
		 *    <topic label="Name" href="link.html"/>
		 *    <topic label="Name" href="link.html">
		 *       <topic label="Name" href="link.html"/>
		 *    </topic>
		 * </toc>
		 */
		private function createTocFromAdobeHhcXml(): void
		{
			var tocXmlString: String = contents;
			var replaceItemTagAndNameAttr:RegExp = /<item name="/g;
			var replaceLinkAttr:RegExp = /link="/g;
			var replaceItemCloseTag:RegExp = /<\/item>/g;
			var replacePropertiesTag:RegExp = /<properties\/>/g;
			
			tocXmlString = tocXmlString.replace(replaceItemTagAndNameAttr, '<topic label="');
			tocXmlString = tocXmlString.replace(replaceLinkAttr, 'href="html/' +  folderName + '/' );
			tocXmlString = tocXmlString.replace(replaceItemCloseTag, '</topic>');
			tocXmlString = tocXmlString.replace(replacePropertiesTag, '');
			tocXmlString = tocXmlString.replace('<?xml version="1.0" encoding="UTF-8"?>', 
				'<?xml version="1.0" encoding="UTF-8"?> \n' +
				'<?NLS TYPE="org.eclipse.help.toc"?> \n' +
				'<!-- ============================== -->\n' +
				'<!-- Define topics for the ' + label + ' -->\n' +
				'<!-- ============================== -->\n');
			
			tocXmlString = tocXmlString.replace('<toc version="1.0">', '<toc label="' + label + '">');
			generatedToc = tocXmlString;
		}

		/**
		 * Converts Adobe's HelpMap.txt into toc.xml. Help Map has following form:
		 * 
		 * __Global__	package-detail.html	__Global__
		 * air.net	air/net/package-detail.html	air.net
		 * 
		 * We need to conert it to TOC XML like the following one:
		 * 	
		 * <?xml version="1.0" encoding="UTF-8"?>
		 * <?NLS TYPE="org.eclipse.help.toc"?>
		 * <toc label="Name">
		 *    <topic label="Name" href="link.html"/>
		 *    <topic label="Name" href="link.html">
		 *       <topic label="Name" href="link.html"/>
		 *    </topic>
		 * </toc>
		 */
		private function createTocFromAdobeHelpMapText(): void
		{
			// TODO FIXME Make more optimal generation, to not include duplicated entries.
			generatedToc = '<?xml version="1.0" encoding="UTF-8"?> \n';
			generatedToc += '<toc label="' + label + '"> \n';
			
			var xmlStr:String;
			var mapLines:Array = contents.split("\n");
			for each (var line:String in mapLines)
			{
				var parts:Array = line.split("\t");
				var label: String = parts[0];
				var href: String = parts[1];
				if (href.indexOf(".htm") == -1)
				{
					//Output.append("\tWrong href entry: " + href);
					continue;
				}
				if (label.indexOf(".htm") != -1)
				{
					//Output.append("\tWrong label entry: " + label);
					continue;
				}
				xmlStr = '\t<topic label="' + label + '" href="html/' +  folderName + '/' + href + '" /> \n';
				generatedToc += xmlStr;
			}
			
			generatedToc += '</toc>';
		}
		
		private function parseFromNone(): void
		{
			Output.append("TODO: Parse TOC Contents from folder structure");
		}
		
		private function createTocSection():void
		{
			if (generatedToc == null)
			{
				return;
			}
			
			var tocTagStartIndex:Number = generatedToc.indexOf("<toc ");
			var tocTagEndIndex:Number = generatedToc.indexOf("</toc>");
			generatedTocSection = generatedToc.substring(tocTagStartIndex, tocTagEndIndex + "</toc>".length);
			
			generatedTocSection = generatedTocSection.replace('<toc ', '<topic '); 
			generatedTocSection = generatedTocSection.replace('</toc>', '</topic>'); 
		}
			
		public function get type():String
		{
			return _type;
		}

		public function set type(value:String):void
		{
			_type = value;
		}

		public function get contents():String
		{
			return _contents;
		}

		public function set contents(value:String):void
		{
			_contents = value;
		}

		public function get generatedToc():String
		{
			return _generatedToc;
		}

		public function set generatedToc(value:String):void
		{
			_generatedToc = value;
		}

		public function get basePath():String
		{
			return _basePath;
		}

		public function set basePath(value:String):void
		{
			_basePath = value;
			
			// Convert to URL (disabled)
			//var replaceSlashRegEx:RegExp = /\\/g;
			//_basePath = _basePath.replace(replaceSlashRegEx, "/");
			//_basePath = "file:///" + _basePath;
		}

		public function get folderName():String
		{
			return _folderName;
		}

		public function set folderName(value:String):void
		{
			_folderName = value;
		}

		public function get fileName():String
		{
			return _fileName;
		}

		[Deprecated]
		public function set fileName(value:String):void
		{
			_fileName = value;
		}

		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			_label = value;
		}

		public function get generatedTocSection():String
		{
			return _generatedTocSection;
		}

		public function set generatedTocSection(value:String):void
		{
			_generatedTocSection = value;
		}
	}
}